package com.msw.ssh.dao;

import com.msw.ssh.model.User;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

/**
 * Created by msw on 2015/10/30.
 */
@Repository
public class UserDao {

    @Resource
    private HibernateTemplate hibernateTemplate;

    public void add(User user)
    {
        hibernateTemplate.save(user);
    }

    /**
     * 测试Hibernate的NamingSQl
     *
     * @param namingSqlId
     * @return
     */
    @Transactional
    public User find(String namingSqlId)
    {
        // 基于hibernate的Named Query
        //return (User)hibernateTemplate.getSessionFactory().getCurrentSession()
        //    .getNamedQuery(namingSqlId).setString("id", "1").list().get(0);

        // 基于Spring进行
        //return (User)this.hibernateTemplate.findByNamedQueryAndNamedParam(namingSqlId, "id", "1").get(0);

        // 从Naming SQl中获取sql语句
       // Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        //String sql = session.getNamedQuery(namingSqlId).getQueryString();
        //System.out.println(sql);

        return null;
    }

}
